﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Daybreak.Core.SceneGraph;

namespace Daybreak.Core
{
    public static class HtmlLog
    {
        private static string fileName = "daybreak.log.htm";
        static bool closed;

        public static bool LogMethods = false;

        static HtmlLog()
        {

            StreamWriter Save = new StreamWriter(fileName, false);
            Save.WriteLine("<html><head>");
            Save.WriteLine("<style type=\"text/css\"><!--");
            Save.WriteLine("body {background-color: #ffffff; color: #000000;}");
            Save.WriteLine("table {border-collapse: collapse;}");
            Save.WriteLine("td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}");
            Save.WriteLine(".h {background-color: #99E5FF; font-weight: bold;}");
            Save.WriteLine(".high {background-color: #C04618; color: #FFFFFF; font-weight: bold; }");
            Save.WriteLine(".med {background-color: #EDAC00; }");
            Save.WriteLine(".low {background-color: #008000;color: #FFFFFF; }");
            Save.WriteLine(".info {background-color: #dddddd; }");
            Save.WriteLine("//--></style></head><body><center>");
            Save.WriteLine("<table border=\"0\" cellpadding=\"3\" width=\"90%\">");
            WriteHeader(Save);

            Save.Close();
        }

        public static void Close()
        {
            if (closed) return;

            StreamWriter Save = new StreamWriter(fileName, true);
            Save.WriteLine("</table></center></body></html>");
            Save.Close();
            closed = true;
        }

        public static void Text(string message, string category)
        {
            Write("info", "", message, category);
        }

        public static void LogCurrentMethod(Node owner)
        {
            if (!LogMethods)
                return;

            StackFrame fram = new StackFrame(1);

            string e = string.Format("({0}).", owner);

            Write("low", "Info", e + fram.GetMethod().Name, fram.GetMethod().DeclaringType.Name);
        }

        public static void Info(string message, string category)
        {
            Write("low", "Info", message, category);

        }

        public static void Warn(string message, string category)
        {
            Write("med", "Warning", message, category);
        }

        public static void Error(string message, string category)
        {
            Write("high", "Error", message, category);
        }

        public static void Error(Exception ex)
        {
            Write("high", "Error", string.Format("{0}<br> Stack trace:<br> {1}", ex.Message, ex.StackTrace), ex.GetType().Name);
        }

        private static void WriteHeader(StreamWriter Save)
        {
            Save.WriteLine("<tr>");
            Save.WriteLine("<td class=\"low\">Date</td>");
            Save.WriteLine("<td class=\"low\">Type</td>");
            Save.WriteLine("<td class=\"low\">Category</td>");
            Save.WriteLine("<td class=\"low\">Message</td>");
            Save.WriteLine("</tr>");

            Save.WriteLine();
            Save.Close();
        }


        private static void Write(string format, string type, string message, string category)
        {
            StreamWriter Save = new StreamWriter(fileName, true);
            Save.WriteLine("<tr>");
            Save.WriteLine("<td class=\"" + format + "\">" + System.DateTime.Now.ToString("HH:mm:ss") + "</td>");
            Save.WriteLine("<td class=\"" + format + "\">" + type + "</td>");
            Save.WriteLine("<td class=\"" + format + "\">" + category + "</td>");
            Save.WriteLine("<td class=\"" + format + "\">" + message + "</td>");
            Save.WriteLine("</tr>");

            Save.WriteLine();
            Save.Close();
        }
    }
}
